<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="dealerEditDialogFormRef" :rules="rules" :model="ruleForm" size="default" label-width="120px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="代理商名称" prop="orgName">
							<el-input v-model="ruleForm.orgName" placeholder="请输入名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="联系电话" prop="orgContact">
							<el-input v-model="ruleForm.orgContact" placeholder="请输入联系电话" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="联系人" prop="userName">
							<el-input v-model="ruleForm.userName" placeholder="请输入联系人姓名" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="手机号" prop="userTel">
							<el-input v-model="ruleForm.userTel" placeholder="请输入联系人手机号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="负责区域" prop="regions">
							<el-cascader :options="options" :props="props" clearable v-model="ruleForm.regions" />
							<!-- <el-select v-model="state.ruleForm.dealerId" placeholder="请选择" clearable class="w100">
								<el-option v-for="item in dealers" :key="item.id" :label="item.name" :value="item.id" />
							</el-select> -->
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="地址" prop="orgAddress">
							<el-input v-model="ruleForm.orgAddress" type="textarea" placeholder="请输入地址" maxlength="150"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts">
import { reactive, ref, nextTick } from 'vue';
import { useDealer, DealerVo } from './dealer';
const props = {
	multiple: true,
	checkStrictly: true,
};
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const options = ref([]);
// 定义变量内容
const dealerEditDialogFormRef = ref();
const ruleForm = reactive<Partial<DealerVo>>({
	id: '', // 用户id
	orgId: '', // 代理商机构id
	orgName: '', // 公司名称
	orgContact: '', // 公司电话
	userName: '', // 联系人
	userTel: '', // 联系电话
	regions: [],
	orgAddress: '', // 地址
});
const state = reactive({
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});
const rules = ref({
	orgName: [
		{ required: true, type: 'string', message: '公司名称不能为空', trigger: 'blur' },
		{ pattern: /[\u4e00-\u9fa5A-Za-z0-9]/, min: 2, max: 20, message: '请输入2-20个中英文字符名称', trigger: 'blur' },
	],
	orgContact: [
		{ required: true, type: 'string', message: '电话不能为空', trigger: 'blur' },
		{ pattern: /[0-9\-（）()]+/, type: 'string', message: '请输入正确的号码格式', trigger: 'blur' },
	],
	userName: [
		{ required: true, type: 'string', message: '联系人不能为空', trigger: 'blur' },
		{ pattern: /[\u4e00-\u9fa5A-Za-z0-9]/, min: 2, max: 20, message: '请输入2-20个中英文字符名称', trigger: 'blur' },
	],
	userTel: [
		{ required: true, type: 'string', message: '联系电话不能为空', trigger: 'blur' },
		{ pattern: /[0-9]{11}/, len: 11, message: '请输入正确的手机号码', trigger: 'blur' },
	],
	orgAddress: [{ required: false, type: 'string', max: 100, message: '地址不能超过100个字符', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (type: string, row: RowUserType) => {
	if (type === 'edit') {
		setFormData(row);
		// ruleForm.value = row;
		state.dialog.title = '修改信息';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增代理商';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		nextTick(() => {
			dealerEditDialogFormRef.value.resetFields();
		});
	}
	state.dialog.isShowDialog = true;
	useDealer()
		.getDivision()
		.then((res: any) => {
			options.value = res ? res : [];
		});
};
const setFormData = (row: any) => {
	ruleForm.id = row.id;
	ruleForm.orgId = row.orgId;
	ruleForm.orgName = row.orgName;
	ruleForm.orgContact = row.orgContact;
	ruleForm.orgAddress = row.orgAddress;
	ruleForm.userName = row.userName;
	ruleForm.userTel = row.userTel;
	ruleForm.regions = row.regions;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	if (!dealerEditDialogFormRef.value) return;
	dealerEditDialogFormRef.value.validate((valid: boolean) => {
		if (valid) {
			useDealer()
				.updateDealer(ruleForm)
				.then(() => {
					closeDialog();
					emit('refresh');
				});
		}
	});
	// closeDialog();
	// emit('refresh');
	// if (state.dialog.type === 'add') { }
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
